package com.example.qcweb;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.example.dao.DBDao;
import com.example.modal.ExamineItem;
import com.example.modal.WorkingProcedure;
import com.example.qc.R;

import java.util.ArrayList;
import java.util.List;

public class ExamItemActivity extends Activity {

    private String SystemId, activityname = "ExamItemActivity";
    private String controlname = null;
    private Intent myAssiIntent;
    private String operator, rechecker, checker, process, army;
    WorkingProcedure workProcedure;
    LayoutInflater inflater;
    Drawable bgdrawable;
    List<ExamineItem> examineItems = new ArrayList<ExamineItem>();

    private void TransferID() {
        // TODO Auto-generated method stub
        myAssiIntent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putString("systemid", SystemId);
        bundle.putString("activityname", activityname);
        bundle.putString("controlname", controlname);
        myAssiIntent.putExtras(bundle);
        Log.i("tag", controlname);
    }

    private void StartAsignActivity() {
        /* 把bundle对象assign给Intent */
        // Intent asignIntent = new Intent();
        // SystemId=
        myAssiIntent.setClass(ExamItemActivity.this, signactivity.class);
        startActivity(myAssiIntent);

    }

    public class ViewHolder {
        public TextView ExamNum;
        public TextView jianchjilu;
        public TextView jianchjildemand;
        public TextView record;
        public TextView conclusion;
        public TextView zhizao;
        public TextView remark;
        public TextView operatorTextView;
        public TextView reInterCheckerTextView;
        public TextView checkerTextView;

    }

    public class ExamAdapter extends BaseAdapter {

        public ExamAdapter(Context context) {
            super();
            inflater = LayoutInflater.from(context);
            // TODO Auto-generated constructor stub
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            if (null != examineItems) {
                return examineItems.size();

            } else {
                return 0;
            }
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int arg0, View arg1, ViewGroup arg2) {
            // TODO Auto-generated method stub
            ViewHolder viewHolder;
            if (arg1 == null) {
                arg1 = inflater.inflate(R.layout.activity_examinecell, null);
                viewHolder = new ViewHolder();
                viewHolder.ExamNum = (TextView) arg1.findViewById(R.id.Examnum);
                // viewHolder.
                viewHolder.jianchjilu = (TextView) arg1
                        .findViewById(R.id.jianchajilu);
                viewHolder.jianchjildemand = (TextView) arg1
                        .findViewById(R.id.jianchjildemand);
                viewHolder.record = (TextView) arg1.findViewById(R.id.record);
                viewHolder.conclusion = (TextView) arg1
                        .findViewById(R.id.conclusion);
                viewHolder.zhizao = (TextView) arg1.findViewById(R.id.tvzhizao);
                viewHolder.remark = (TextView) arg1
                        .findViewById(R.id.remarktext);
                viewHolder.operatorTextView = (TextView) arg1
                        .findViewById(R.id.tvrealexamoperator);
                viewHolder.reInterCheckerTextView = (TextView) arg1
                        .findViewById(R.id.tvexamrechecker);
                viewHolder.checkerTextView = (TextView) arg1
                        .findViewById(R.id.tvexamchecker);
                arg1.setTag(viewHolder);

            } else {
                viewHolder = (ViewHolder) arg1.getTag();
            }
            ExamineItem examineItem = examineItems.get(arg0);
            viewHolder.ExamNum.setText(examineItem.getSystemId());

            viewHolder.jianchjilu.setText(examineItem.getItem());
            viewHolder.jianchjildemand.setText(examineItem
                    .getCapabilityTarget());
            viewHolder.record.setText(examineItem.getWatchValue());
            viewHolder.conclusion.setText(examineItem.getConclusion());
            viewHolder.zhizao.setText(examineItem.getMakeRes());
            viewHolder.remark.setText(examineItem.getRemark());
            viewHolder.operatorTextView.setText(examineItem.getOperator());
            viewHolder.reInterCheckerTextView.setText(examineItem
                    .getReInterChecker());
            viewHolder.checkerTextView.setText(examineItem.getChecker());

            SystemId = examineItem.getSystemId();
            //	Log.i("systemid", SystemId);
            operator = examineItem.getOperator();
            //	Log.i("operator", operator);
            rechecker = examineItem.getReInterChecker();
            //	Log.i("rechecker", rechecker);
            checker = examineItem.getChecker();

            //	Log.i("tag", checker);

            viewHolder.operatorTextView
                    .setOnClickListener(LvButtonOnClickListener);
            viewHolder.reInterCheckerTextView
                    .setOnClickListener(LvButtonOnClickListener);
            viewHolder.checkerTextView
                    .setOnClickListener(LvButtonOnClickListener);
            return arg1;
        }

        View.OnClickListener LvButtonOnClickListener = new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                switch (v.getId()) {
                    case R.id.tvrealexamoperator:
                        if (operator.equals(null)) {

                            controlname = "operator";
                            TransferID();
                            StartAsignActivity();
                        } else {

                            controlname = "operator";
                            TransferID();
                            StartAsignActivity();

                            new AlertDialog.Builder(ExamItemActivity.this)
                                    .setTitle("警告提示").setMessage("请检验员刷卡取消签字")
                                    .setPositiveButton("确定", null).show();
                            // StartAsignActivity(SystemId);
                        }
                        break;

                    case R.id.tvexamrechecker:
                        if (operator.equals(null) & rechecker.equals(null)) {

                            controlname = "rechecker";
                            TransferID();
                            StartAsignActivity();

                        } else {
                            new AlertDialog.Builder(ExamItemActivity.this)
                                    .setTitle("警告提示").setMessage("请检验员刷卡取消签字")
                                    .setPositiveButton("确定", null).show();

                        }
                        break;

                    case R.id.tvexamchecker:
                        if (checker.equals(null)) {
                            controlname = "checker";
                            TransferID();
                            StartAsignActivity();
                        } else {

                            new AlertDialog.Builder(ExamItemActivity.this)
                                    .setTitle("警告提示").setMessage("请检验员刷卡取消签字")
                                    .setPositiveButton("确定", null).show();

                        }

                        break;
                }
            }
        };

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // 无标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_examine);
        // bgdrawable=getResources().getDrawable(R.drawable.bgpartexammdedia);
        // LinearLayout layout3=(LinearLayout)findViewById(R.id.Layout3);
        // layout3.setBackgroundDrawable(bgdrawable);
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        workProcedure = (WorkingProcedure) bundle.getSerializable("key");
        //SystemId = workProcedure.getSystemId().toString();
        //Log.i("tag", SystemId);
        findViews();
        getData();

    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        bgdrawable = null;
    }

    private void getData() {
        // TODO Auto-generated method stub
        examineItems = DBDao.findExamItems(workProcedure.getKEYCardCode(),
                workProcedure.getSystemId());
    }

    private void findViews() {
        // TODO Auto-generated method stub
        TextView name = (TextView) findViewById(R.id.examtext);
        name.setText(MyGlobal.KeycardCode + "-工序" + workProcedure.getProduce()
                + "-工步" + workProcedure.getStep() + "实测记录表");
        ListView examList = (ListView) findViewById(R.id.examlist);
        //ListView examList  = (ListView) findViewById(R.id.examlist);
        ExamAdapter examadapter = new ExamAdapter(ExamItemActivity.this);
        examList.setAdapter(examadapter);
        ImageButton examinebtn = (ImageButton) findViewById(R.id.Examinebtn);
        examinebtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent();
                intent.setClass(ExamItemActivity.this, OperateActivity.class);
                intent.putExtra("type", "ExamItem");
                intent.putExtra("key1", workProcedure);
                startActivity(intent);
            }
        });
        // Drawable background=getResources().getDrawable(R.drawable.bgprocess);
        // ImageButton Imgbtnoperate=new ImageButton(ExamItemActivity.this);
        // Imgbtnoperate.setBackground(background);
    }

}
